package com.person.jc.web.buy;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.buy.BsBuyApplyService;
import com.person.jc.service.buy.BsBuyAuditService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;

public class BsBuyAuditController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		setAttr("startDate",DateUtils.formatDate(DateUtils.addMonths(new Date(), -1), "yyyy-MM-dd"));
		
		setAttr("endDate",DateUtils.getDate());
		render("main.jsp");
	}
	
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BsBuyAuditService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	/**
	 * 主管采购审批（首页）
	 */
	public void getApplyCount(){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		List<Record> list = BsBuyAuditService.me.getApplyCount(tsUser.getUserId());
		setAttr("applyDirCount", list!=null?list.size():0);
		List<Record> list1 = BsBuyAuditService.me.getApplyMaCount(tsUser.getUserId());
		setAttr("applyManageCount", list1!=null?list1.size():0);
		render("indexApply.jsp");
	}
	
	/**
	 * 主管采购审批（首页）
	 */
	public void getDirCheckCount(){
		List<Record> list = BsBuyAuditService.me.getDirCheckCount();
		setAttr("dirCount", list!=null?list.size():0);
		render("indexDirCheck.jsp");
	}
	
	/**
	 * 总经理采购审批（首页）
	 */
	public void getManCheckCount(){
		List<Record> list = BsBuyAuditService.me.getManCheckCount();
		setAttr("manCount", list!=null?list.size():0);
		render("indexManCheck.jsp");
	}
	
	public void edit(){
		String buyId = getPara("buyId");
		String view = getPara("view");
		String auditType = getPara("auditType");
		if(Utils.isNotEmpty(buyId)){
			Record buy = BsBuyApplyService.me.getBuyInfo(buyId);
			List<Record> details = BsBuyApplyService.me.getBuyDetail(buyId);
			setAttr("obj", buy);
			setAttr("details", details);
		}
		setAttr("auditType", auditType);
		setAttr("view", view);
		render("edit.jsp");
	}
	
	public void save(){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		String buyId = getPara("p_buyId");
		String auditType = getPara("p_auditType");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buyId", buyId);
		map.put("optUser", tsUser.getUserId());
		if("1".equals(auditType)){
			BsBuyAuditService.me.saveDirAudit(map);
		}else if("2".equals(auditType)){
			BsBuyAuditService.me.saveMgrAudit(map);
		}
		renderText("success");
	}
}
